#!/bin/bash
#
#  Uruk Updater
#
#  Copyright 2022 hayder majid <hayder@riseup.net>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, you can download it from here:
#  https://www.gnu.org/licenses/gpl-3.0.html
#


#Check if you run this script with root privileges
if [ "$(whoami)" != "root" ]; then
    echo "You need to be in root privileges to run this script. Exiting."
    exit 1
fi

#set the working dir
WORKDIR=$(pwd)

#main installer function
install(){
	echo "Installing Dependencies"
	apt install gir1.2-ayatanaappindicator3-0.1 libayatana-appindicator3-1 gir1.2-gtk-3.0 zenity python3-gi python3-tk synaptic libnotify-bin
	if [ ! -d "/usr/lib/urukUpdater" ]; then
	    mkdir /usr/lib/urukUpdater;
	    cp ${WORKDIR}/usr/lib/urukUpdater/* /usr/lib/urukUpdater;
	else
	    rm -rf /usr/lib/urukUpdater/*
	    cp ${WORKDIR}/usr/lib/urukUpdater/* /usr/lib/urukUpdater;
	fi
	
	if [ ! -d "/usr/share/urukUpdater" ]; then
	    mkdir /usr/share/urukUpdater;
	    cp -r ${WORKDIR}/usr/share/urukUpdater/* /usr/share/urukUpdater;
	else
	    rm -rf /usr/share/urukUpdater/*;
	    cp -r ${WORKDIR}/usr/share/urukUpdater/* /usr/share/urukUpdater;
	fi
	
	if [ ! -f "/usr/share/applications/urukupdater.desktop" ]; then
	    cp ${WORKDIR}/urukupdater.desktop /usr/share/applications;
	else
	    cp --force ${WORKDIR}/urukupdater.desktop /usr/share/applications;
	fi

	if [ ! -f "/usr/share/pixmaps/urukupdater.svg" ]; then
	    cp ${WORKDIR}//usr/share/pixmaps/urukupdater.svg /usr/share/pixmaps/;
	else
	    cp --force ${WORKDIR}//usr/share/pixmaps/urukupdater.svg /usr/share/pixmaps/;
	fi
	
	if [ ! -f "/usr/bin/urukupdater" ]; then
	    cp ${WORKDIR}/usr/bin/urukupdater /usr/bin/;
	else
	    cp --force ${WORKDIR}/usr/bin/urukupdater /usr/bin/;
	fi
	if [ ! -f "/etc/sudoers.d/urukupdater" ]; then
	    cp ${WORKDIR}/etc/sudoers.d/urukupdater /etc/sudoers.d/;
	else
	    cp --force ${WORKDIR}/etc/sudoers.d/urukupdater /etc/sudoers.d/;
	fi
	if [ ! -f "/etc/xdg/autostart/urukupdater.desktop" ]; then
	    cp ${WORKDIR}/etc/xdg/autostart/urukupdater.desktop /etc/xdg/autostart;
	else
	    cp --force ${WORKDIR}/etc/xdg/autostart/urukupdater.desktop /etc/xdg/autostart;
	fi
	chmod +x /usr/bin/urukupdater
	chmod +x /usr/share/urukUpdater/*.py
	chmod +x /usr/share/urukUpdater/updater-status
	chmod +x /usr/lib/urukUpdater/*
	chmod +x /etc/sudoers.d/urukupdater
}

#uninstaller function

uninstall(){
	if [ -d "/usr/lib/urukUpdater" ]; then
	    rm -rf /usr/lib/urukUpdater;
	fi
	
	if [ -d "/usr/share/urukUpdater" ]; then
	    rm -rf /usr/share/urukUpdater;
	fi
	
	if [ -f "/usr/share/applications/urukupdater.desktop" ]; then
	    rm -rf /usr/share/applications/urukupdater.desktop;
	fi

	if [ -f "/usr/share/pixmaps/urukupdater.svg" ]; then
	    rm -rf /usr/share/pixmaps/urukupdater.svg;
	fi
	
	if [ -f "/usr/bin/urukupdater" ]; then
	    rm -rf /usr/bin/urukupdater;
	fi
	if [ -f "/etc/sudoers.d/urukupdater" ]; then
	    rm -rf /etc/sudoers.d/urukupdater;
	fi
	if [ -f "/etc/xdg/autostart/urukupdater.desktop" ]; then
	    rm -rf /etc/xdg/autostart/urukupdater.desktop;
	fi
}

#translate(){
	
	
	
#}

if [ "$1" = "help" ]; then
        echo "  This script will help you to install or uninstall for Uruk Updater"
        echo " "
        echo "  Usage this script as follows:"
        echo " "
        #echo "  sudo ./installer help|install|uninstall|translate"
        echo "  sudo ./installer help|install|uninstall"
        echo " "
        echo " "
        echo "  Examples:"
        echo " "
        echo " "
        echo "   sudo ./installer install ==> (to install urukupdater on your system)"
        echo " "
        echo "   sudo ./installer uninstall  ==> (to remove uninstall urukupdater from your system)"
        #echo " "
        #echo "   sudo ./installer  translate ==> (to generate translation files)"
        echo " "
        echo "   sudo ./installer help ==> (to show this help instructions )"

        echo " "
        echo "   for more information, you can see to official page:"
        echo "   https://www.notabug.org/hayderctee/urukupdater"
        echo " "

elif [ "$1" = "" ]; then
        echo "  you need to use one argument to use this script"
        echo " "
        echo "  Usage of installer script is as follows:"
        echo " "
        echo "   sudo ./installer install ==> (to install urukupdater on your system)"
        echo " "
        echo "   sudo ./installer uninstall  ==> (to remove uninstall urukupdater from your system)"
        echo " "
        #echo "   sudo ./installer  translate ==> (to generate translation files)"
        echo " "
        echo " sudo ./installer help ===> for more info"
elif [ "$1" = "install" ]; then
    echo "installing Uruk Updater . . ."
    install
    echo "done"
elif [ "$1" = "uninstall" ]; then
    echo "uninstalling Uruk Updater . . ."
    echo ""
    uninstall
    echo "done"
#elif [ "$1" = "translate" ]; then
    #echo "generating translate files . . ."
    #echo ""
    #translate
else
    echo "Use valid input command . . ."
	echo "  you need to use one argument to use this script"
	echo " "
	echo "  Usage of installer script is as follows:"
	echo " "
	echo "   sudo ./installer install ==> (to install urukupdater on your system)"
	echo " "
	echo "   sudo ./installer uninstall  ==> (to remove uninstall urukupdater from your system)"
	echo " "
	#echo "   sudo ./installer  translate ==> (to generate translation files)"
	echo " "
	echo " sudo ./installer help ===> for more info"
fi
